home *** CD-ROM | disk | FTP | other *** search
/ SGI Performance Co-Pilot 1.3 / SGI Performance Co-Pilot 1.3.iso / dist / pcp.idb / usr / pcp / bin / pmnewlog.z / pmnewlog
Text File  |  1997-04-03  |  9KB  |  436 lines

  1. #!/bin/sh
  2. #
  3. # Copyright 1995, Silicon Graphics, Inc.
  4. # ALL RIGHTS RESERVED
  5. # UNPUBLISHED -- Rights reserved under the copyright laws of the United
  6. # States.   Use of a copyright notice is precautionary only and does not
  7. # imply publication or disclosure.
  8. # U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
  9. # Use, duplication or disclosure by the Government is subject to restrictions
  10. # as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
  11. # in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
  12. # in similar or successor clauses in the FAR, or the DOD or NASA FAR
  13. # Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
  14. # 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
  15. # THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
  16. # INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
  17. # DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
  18. # PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
  19. # GRAPHICS, INC.
  20. #
  21. # stop and restart a pmlogger instance
  22. #
  23. # $Id: pmnewlog.sh,v 2.1 1997/03/26 03:05:37 kenmcd Exp $
  24.  
  25. tmp=/tmp/$$
  26. status=0
  27. trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15
  28. prog=`basename $0`
  29.  
  30. _abandon()
  31. {
  32.     echo
  33.     echo "Sorry, but this is fatal.  No new pmlogger instance has been started."
  34.     status=1
  35.     exit
  36. }
  37.  
  38. _check_pid()
  39. {
  40.     ps -ef \
  41.     | nawk '
  42. /<defunct>/    { next }
  43. $2 == '$1'    { print $2 }'
  44. }
  45.  
  46. _check_logfile()
  47. {
  48.     if [ ! -f $logfile ]
  49.     then
  50.     echo "Cannot find pmlogger output file at \"$logfile\""
  51.     else
  52.     echo "Contents of pmlogger output file \"$logfile\" ..."
  53.     cat $logfile
  54.     fi
  55. }
  56.  
  57. _check_logger()
  58. {
  59.     # wait until pmlogger process starts, or exits
  60.     #
  61.     delay=5
  62.     [ ! -z "$PMCD_CONNECT_TIMEOUT" ] && delay=$PMCD_CONNECT_TIMEOUT
  63.     x=5
  64.     [ ! -z "$PMCD_REQUEST_TIMEOUT" ] && x=$PMCD_REQUEST_TIMEOUT
  65.  
  66.     # wait for maximum time of a connection and 20 requests
  67.     #
  68.     delay=`expr $delay + 20 \* $x`
  69.     i=0
  70.     while [ $i -lt $delay ]
  71.     do
  72.     echo ".\c"
  73.     if echo "connect $1" | pmlc >$tmp.out 2>&1
  74.     then
  75.         if grep "Unable to connect" $tmp.out >/dev/null
  76.         then
  77.         :
  78.         else
  79.         sleep 5
  80.         echo " done"
  81.         return 0
  82.         fi
  83.     fi
  84.     if ps -ef | grep pmlogger | nawk '
  85. BEGIN        { sts=0 }
  86. $2 == '$1'    { sts=1; exit sts }
  87. END        { exit sts }'
  88.     then
  89.         echo " process exited!"
  90.         _check_logfile
  91.         return 1
  92.     fi
  93.     sleep 5
  94.     i=`expr $i + 5`
  95.     done
  96.     echo " timed out waiting!"
  97.     sed -e 's/^/    /' $tmp.out
  98.     _check_logfile
  99.     return 1
  100. }
  101.  
  102. primary=true
  103. myname="primary pmlogger"
  104. connect=primary
  105. access=""
  106. config=""
  107. saveconfig=""
  108. logfile="pmlogger.log"
  109. namespace=""
  110. args=""
  111. sock_me=""
  112. usage="Usage: $prog"' [-a accessfile] [-c configfile] [-C saveconfig]
  113.     [-l logfile] [-L] [-n namespace] [-p pid] [-s] [-v volsamples] archive'
  114.  
  115. # option parsing
  116. #
  117. while getopts a:c:C:l:Ln:p:sv: c
  118. do
  119.     case $c
  120.     in
  121.     a)    access="$OPTARG"
  122.         if [ ! -f $access ]
  123.         then
  124.             echo "$prog: Error: cannot find accessfile ($access)"
  125.             _abandon
  126.         fi
  127.         ;;
  128.  
  129.     c)    config="$OPTARG"
  130.         if [ ! -f $config ]
  131.         then
  132.             if [ -f /var/pcp/config/pmlogger/$config ]
  133.             then
  134.             config="/var/pcp/config/pmlogger/$config"
  135.             else
  136.             echo "$prog: Error: cannot find configfile ($config)"
  137.             _abandon
  138.             fi
  139.         fi
  140.         ;;
  141.  
  142.     C)    saveconfig="$OPTARG"
  143.         ;;
  144.  
  145.     l)    logfile="$OPTARG"
  146.         ;;
  147.  
  148.     L)    args="$args-L "
  149.         ;;
  150.  
  151.     n)
  152.         namespace="-n $OPTARG"
  153.         args="$args-$c $OPTARG "
  154.         ;;
  155.  
  156.     p)    pid=$OPTARG
  157.         primary=false
  158.         myname="pmlogger (pid $pid)"
  159.         connect=$pid
  160.         ;;
  161.  
  162.     s)
  163.         sock_me="pmsocks "
  164.         ;;
  165.  
  166.     v)
  167.         args="$args-$c $OPTARG "
  168.         ;;
  169.     
  170.     \?)    echo "$usage"
  171.         _abandon
  172.         ;;
  173.     esac
  174. done
  175. shift `expr $OPTIND - 1`
  176.  
  177. if [ $# -ne 1 ]
  178. then
  179.     echo "$usage"
  180.     _abandon
  181. fi
  182.  
  183. # initial sanity checking for new archive name
  184. #
  185. archive=$1
  186. if [ -f $archive.0 -o -f $archive.index -o -f $archive.meta ]
  187. then
  188.     echo "$prog: Error: archive \"$archive\" exists, pmlogger would fail on restart"
  189.     ls -l $archive.*
  190.     _abandon
  191. fi
  192.  
  193. # check that designated pmlogger is really running
  194. #
  195. echo "Looking for $myname ...\c"
  196. ps -ef \
  197. | if $primary
  198.   then
  199.     grep 'pmlogger .*-P' | grep -v grep
  200.   else
  201.     nawk '$2 == '"$pid"' && /pmlogger/ { print }'
  202.   fi >$tmp.out
  203.  
  204. if [ -s $tmp.out ]
  205. then
  206.     echo " found"
  207.     cat $tmp.out
  208.     pid=`nawk '{ print $2 }' <$tmp.out`
  209. else
  210.     echo " Error: process not found"
  211.     _abandon
  212. fi
  213.  
  214. # pass primary/not primary down
  215. #
  216. $primary && args="$args-P "
  217.  
  218. # pass logfile option down
  219. #
  220. args="$args-l $logfile "
  221.  
  222. # if not a primary pmlogger, get name of pmcd host pmlogger is connected to
  223. #
  224. if $primary
  225. then
  226.     :
  227. else
  228.     # start critical section ... no interrupts due to pmlogger SIGPIPE
  229.     # bug in PCP 1.1
  230.     #
  231.     trap "echo; echo $prog:' Interrupt! ... I am talking to pmlogger, please wait ...'" 1 2 3 15
  232.     echo "Getting logged host name from $myname ...\c"
  233.  
  234.     ( echo "connect $connect" ; echo "status" ) \
  235.     | pmlc 2>$tmp.err >$tmp.out
  236.  
  237.     # end critical section
  238.     #
  239.     trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15
  240.  
  241.     if [ -s $tmp.err ]
  242.     then
  243.     if grep "Unable to connect" $tmp.err >/dev/null
  244.     then
  245.         echo " failed to connect"
  246.         echo
  247.         sed -e 's/^/    /' $tmp.err
  248.         _abandon
  249.     else
  250.         echo
  251.         echo "$prog: Warning: errors from talking to $myname via pmlc"
  252.         sed -e 's/^/    /' $tmp.err
  253.         echo
  254.         echo "continuing ..."
  255.     fi
  256.     else
  257.     echo " done"
  258.     fi
  259.  
  260.     host=`sed -n -e '/^pmlogger/s/.* from host //p' <$tmp.out`
  261.     if [ "X$host" = X ]
  262.     then
  263.     echo "$prog: Error: failed to get host name from $myname"
  264.     echo "This is what was collected from $myname."
  265.     echo
  266.     sed -e 's/^/    /' $tmp.out
  267.     _abandon
  268.     fi
  269.     args="$args-h $host "
  270. fi
  271.  
  272. # extract/contruct config file if required
  273. #
  274. if [ "X$config" = X ]
  275. then
  276.     # start critical section ... no interrupts due to pmlogger SIGPIPE
  277.     # bug in PCP 1.1
  278.     #
  279.     trap "echo; echo $prog:' Interrupt! ... I am talking to pmlogger, please wait ...'" 1 2 3 15
  280.     echo "Contacting $myname to get logging state ...\c"
  281.  
  282.     # iterate over top-level names in pmns, and query pmlc for
  283.     # current configuration ... note exclusion of "proc" metrics
  284.     # ... others may be excluded in a similar fashion
  285.     #
  286.     ( echo "connect $connect" \
  287.       ; for top in `pminfo $namespace \
  288.             | sed -e 's/\..*//' -e '/^proc$/d' \
  289.             | sort -u`
  290.     do
  291.         echo "query $top"
  292.     done \
  293.     ) \
  294.     | pmlc $namespace 2>$tmp.err \
  295.     | nawk >$tmp.out '
  296. /^[^ ]/                        { metric = $1; next }
  297. $1 == "mand" || ( $1 == "adv" && $2 == "on" )     { print $0 " " metric }'
  298.  
  299.     # end critical section
  300.     #
  301.     trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15
  302.  
  303.     if [ -s $tmp.err ]
  304.     then
  305.     if grep "Unable to connect" $tmp.err >/dev/null
  306.     then
  307.         echo " failed to connect"
  308.         echo
  309.         sed -e 's/^/    /' $tmp.err
  310.         _abandon
  311.     else
  312.         echo
  313.         echo "$prog: Warning: errors from talking to $myname via pmlc"
  314.         sed -e 's/^/    /' $tmp.err
  315.         echo
  316.         echo "continuing ..."
  317.     fi
  318.     else
  319.     echo " done"
  320.     fi
  321.  
  322.     if [ ! -s $tmp.out ]
  323.     then
  324.     echo "$prog: Error: failed to collect configuration info from $myname"
  325.     echo "Most likely this pmlogger instance is inactive."
  326.     _abandon
  327.     fi
  328.  
  329.     # convert to a pmlogger config file
  330.     #
  331.     sed <$tmp.out >$tmp.config \
  332.     -e 's/ on  nl/ on/' \
  333.     -e 's/ off nl/ off/'\
  334.     -e 's/  *mand  *\(o[nf]*\) /log mandatory \1 /' \
  335.     -e 's/  *adv  *on /log advisory on/' \
  336.     -e 's/\[[0-9][0-9]* or /[/' \
  337.     -e 's/\(\[[^]]*]\) \([^ ]*\)/\2 \1/' \
  338.     -e 's/   */ /g'
  339.  
  340.     if [ ! -s $tmp.config ]
  341.     then
  342.     echo "$prog: Error: failed to generate a pmlogger configuration file for pmlogger"
  343.     echo "This is what was collected from $myname."
  344.     echo
  345.     sed -e 's/^/    /' $tmp.out
  346.     _abandon
  347.     fi
  348.     config=$tmp.config
  349. fi
  350.  
  351. # optionally append access control specifications
  352. #
  353. if [ "X$access" != X ]
  354. then
  355.     if grep '\[access]' $config >/dev/null
  356.     then
  357.     echo "$prog: Error: pmlogger configuratiuon file already contains an"
  358.     echo "    access control section, specifications from \"$access\" cannot"
  359.     echo "    be applied."
  360.     _abandon
  361.     fi
  362.     cat $access >>$config
  363. fi
  364.  
  365. # add config file to the args, save config file if -C
  366. #
  367. args="$args-c $config "
  368. if [ "X$saveconfig" != X ]
  369. then
  370.     if cp $config $saveconfig
  371.     then
  372.     echo "New pmlogger configuration file saved as $saveconfig"
  373.     else
  374.     echo "$prog: Warning: unable to save configuration file as $saveconfig"
  375.     fi
  376. fi
  377.  
  378. # kill off existing pmlogger
  379. #
  380. echo "Terminating $myname ...\c"
  381. for sig in INT HUP KILL
  382. do
  383.     echo " SIG$sig ...\c"
  384.     kill -$sig $pid
  385.     sleep 1
  386.     [ "`_check_pid $pid`" = "" ] && break
  387. done
  388.  
  389. # the archive folio Latest is for the most recent archive in this directory
  390. #
  391. dir=`dirname $archive`
  392. rm -f $dir/Latest
  393.  
  394. if [ "`_check_pid $pid`" = "" ]
  395. then
  396.     echo " done"
  397. else
  398.     echo " failed!"
  399.     _abandon
  400. fi
  401.  
  402. # clean up port-map, just in case
  403. #
  404. PM_LOG_PORT_DIR=/var/tmp/pmlogger
  405. rm -f $PM_LOG_PORT_DIR/$pid
  406. $primary && rm -f $PM_LOG_PORT_DIR/primary $PM_LOG_PORT_DIR/vcr
  407.  
  408. # finally do it, ...
  409. #
  410. cd $dir
  411. [ "X$dir" = X. ] && dir=`pwd`
  412. archive=`basename $archive`
  413. echo "Launching new pmlogger in directory \"$dir\" as ..."
  414. [ -f $logfile ] && mv -f $logfile $logfile.prior
  415. echo "${sock_me}/usr/pcp/bin/pmlogger $args$archive"
  416. ${sock_me}/usr/pcp/bin/pmlogger $args$archive &
  417. new_pid=$!
  418.  
  419. echo "Waiting for new pmlogger to start ..\c"
  420. if _check_logger $new_pid
  421. then
  422.     echo "New pmlogger status ..."
  423.     ( echo "connect $new_pid"; echo "status" ) | pmlc
  424.  
  425.     # make the "Latest" archive folio
  426.     #
  427.     [ -f $archive.0 ] && /usr/pcp/bin/mkaf $archive.0 >Latest
  428.  
  429.     exit
  430. else
  431.     _abandon
  432. fi
  433.